//
//  AllCommentCellTableViewCell.swift
//  iModern
//
//  Created by 立雄 王 on 15/8/28.
//  Copyright (c) 2015年 com.starlooking. All rights reserved.
//

import UIKit

class AllCommentCellTableViewCell: TableViewCell {
    
    let __comment_label_tags       = 50
    let __comment_image_tags       = 51
    let __Avatar_label_tags        = 52
    let __button_tag               = 53
    let __all_button_tag           = 54
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        initCommentLabel()
        initAvatarImageView()
        initAvatarLabel()
        initButton()
        initLabel()
        
    }
    
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    func setData(data: NSMutableDictionary)
    {
        //let advertising = data["advertising"] as! [String:AnyObject]
        let contact     = data["contact"] as! [String:AnyObject]
        //let assignedTo  = data["assignedTo"] as! [String:AnyObject]
        
        let score       = data["score"] as! Int
        let review      = data["review"] as! String
        
        //        var label = self.viewWithTag(__comment_label_tags) as? UILabel
        //        label?.text = review
        setCommentLabel(review)
        setAvatarImageView(contact["thumbnailURL"] as? String)
        
        let label2 = self.viewWithTag(__Avatar_label_tags) as? UILabel
        label2?.text =  contact["alias"] as? String
        
        let button = self.viewWithTag(__button_tag) as? UILabel
        if score == 0
        {
            button!.backgroundColor = UIColor(red: 245 / 225, green: 154 / 225, blue: 47 / 225, alpha: 1)
            button?.text = "一般"
        }
        if score == 1
        {
            button?.text = "不错"
            button!.backgroundColor = UIColor(red: 122 / 225, green: 179 / 225, blue: 61 / 225, alpha: 1)
        }
        if score == 2
        {
            button?.text = "超赞"
            button!.backgroundColor = UIColor(red: 215 / 225, green: 51 / 225, blue: 155 / 225, alpha: 1)
        }
        if score == 3
        {
            button?.text = "神作"
            button!.backgroundColor = UIColor(red: 215 / 225, green: 51 / 225, blue: 155 / 225, alpha: 1)
        }
        if score == -1
        {
            button?.text = "没评"
            button!.backgroundColor = UIColor.whiteColor()
        }
    }
    
    func setCommentLabel(review: String?)
    {
        //let messageHeight = sizeOfText(review!).height
        let label = self.viewWithTag(__comment_label_tags) as? UILabel
        label?.text = review
        label!.numberOfLines=3
        label!.adjustsFontSizeToFitWidth = true
    }
    
    func setAvatarImageView(thumbnailURL: String?)
    {
        let imageView = self.viewWithTag(__comment_image_tags) as! UIImageView
        if thumbnailURL != nil
        {
            let url = appDelegate.parameterManager.server + "/" + thumbnailURL!
            imageView.load(url, placeholder: nil)
        }
        else
        {
            imageView.image = UIImage(named: "Avatar-Default.png")
        }
    }
    
    func initCommentLabel()
    {
        let screen = UIScreen.mainScreen().bounds
        var label = self.viewWithTag(__comment_label_tags) as? UILabel
        if (label == nil)
        {
            let frame = CGRect(x: 10, y: 10, width: screen.width - 15, height: 40)
            label = UILabel(frame: frame)
            label!.font = UIFont.systemFontOfSize(14)
            label!.tag = __comment_label_tags
            label?.text = "包装用心，礼物好精美，东西非常非常棒。膜拜中"
            label?.textColor = UIColor.blackColor()
            self.addSubview(label!)
        }
    }
    
    func initAvatarImageView()
    {
        var imageView = self.viewWithTag(__comment_image_tags) as? UIImageView
        if (imageView == nil)
        {
            let frame = CGRect(x: 10.0, y:60.0, width: 40.0, height: 40.0)
            imageView = UIImageView(frame: frame)
            imageView!.image = UIImage(named: "Avatar-Default.png")
            imageView!.tag = __comment_image_tags
            imageView!.layer.cornerRadius = 20.0
            imageView!.layer.masksToBounds = true
            imageView!.layer.borderColor = UIColor.blackColor().CGColor
            imageView!.layer.borderWidth = 1.0
            self.addSubview(imageView!)
        }
        
    }
    
    func initAvatarLabel() //160
    {
        var label = self.viewWithTag(__Avatar_label_tags) as? UILabel
        if (label == nil)
        {
            let frame = CGRect(x: 70, y: 60, width: 100, height: 40)
            label = UILabel(frame: frame)
            label!.font = UIFont.systemFontOfSize(14)
            label!.tag = __Avatar_label_tags
            label?.text = "包装用心"
            label?.textColor = UIColor.blackColor()
            self.addSubview(label!)
        }
    }
    
    func initButton()
    {
        let screen = UIScreen.mainScreen().bounds
        var button = self.viewWithTag(__button_tag) as? UILabel
        if (button == nil)
        {
            let frame = CGRect(x: screen.width - 55, y: 70, width: 40, height: 20)
            button = UILabel(frame: frame)
            button!.font = UIFont.systemFontOfSize(14)
            button?.textColor = UIColor.whiteColor()
            button!.backgroundColor = UIColor(red: 245 / 225, green: 154 / 225, blue: 47 / 225, alpha: 1)
            button?.text = "一般"
            button!.tag = __button_tag
            button!.layer.borderWidth = 1.0
            button!.layer.borderColor = UIColor.whiteColor().CGColor
            button!.layer.masksToBounds = true
            button!.layer.cornerRadius = 8
            button?.textAlignment = NSTextAlignment.Center
            self.addSubview(button!)
        }
    }
    
    func initLabel()
    {
        let screen = UIScreen.mainScreen().bounds
        //let y     = bounds.height - 35
        //let x     = bounds.width - 145
        var label      = UILabel()
        //let frame = CGRect(x: x, y: y, width: 140, height: 30)
        let labelFrame = CGRect(x: 0, y: 1, width: screen.size.width, height: 1)
        label = UILabel(frame: labelFrame)
        label.backgroundColor = UIColor.groupTableViewBackgroundColor()
        self.addSubview(label)
    }
    
    func sizeOfText(text: NSString) -> CGSize
    {
        let paragraphStyle = NSMutableParagraphStyle()
        
        paragraphStyle.lineHeightMultiple = 16
        paragraphStyle.maximumLineHeight = 20
        paragraphStyle.minimumLineHeight = 20
        
        //        paragraphStyle.firstLineHeadIndent = 20
        paragraphStyle.alignment = .Left
        
        let attributes = [NSFontAttributeName: UIFont.systemFontOfSize(14),
            NSParagraphStyleAttributeName: paragraphStyle,
            NSForegroundColorAttributeName: UIColor.darkGrayColor()]
        
        let frame = CGSizeMake(screen.size.width, screen.size.height)
        //let options =  [NSStringDrawingOptions.TruncatesLastVisibleLine,NSStringDrawingOptions.UsesLineFragmentOrigin , NSStringDrawingOptions.UsesFontLeading]
        let options = unsafeBitCast(NSStringDrawingOptions.UsesLineFragmentOrigin.rawValue |
            NSStringDrawingOptions.UsesFontLeading.rawValue,
            NSStringDrawingOptions.self)
        let size  = text.boundingRectWithSize(frame, options: options, attributes: attributes, context: nil)
        return size.size
    }
    
}
